﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.ComponentModel;

namespace Kokomo.Mirror.PeFormat.Resources
{
	public class ResourceItem
	{
		public ResourceItem(
			PeModule module,
			ResourcePath path
			)
		{
			if (module == null) throw new ArgumentNullException("module");
			if (path == null) throw new ArgumentNullException("path");

			this.Module = module;
			this.Path = path;
		}

		[Browsable(false)]
		public PeModule Module { get; private set; }
		public ResourcePath Path { get; private set; }

		[DisplayName("Predefined type")]
		public ResourceType PredefinedType
		{
			get
			{
				if (this.Path.Parts.Length < 1) return ResourceType.Unknown;

				ResourceIdPart idPart = this.Path.Parts[0] as ResourceIdPart;
				if (idPart == null) return ResourceType.Unknown;

				bool isPredefinedType = Enum.IsDefined(typeof(ResourceType), (int)idPart.ID);
				if (!isPredefinedType) return ResourceType.Unknown;

				return (ResourceType)idPart.ID;
			}
		}

		private CultureInfo _culture;
		public CultureInfo Culture
		{
			get
			{
				if (this._culture != null) return this._culture;

				if (this.Path.Parts.Length < 3) return null;

				ResourceIdPart idPart = this.Path.Parts[2] as ResourceIdPart;
				if (idPart == null) return null;

				int cultureCode = idPart.ID;
				CultureInfo culture = null;
				if (cultureCode != 0)
				{
					try { culture = CultureInfo.GetCultureInfo(cultureCode); }
					catch { }
				}

				this._culture = culture;

				return culture;
			}
		}
	}
}
